package com.yizhubao.app.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.yizhubao.app.R;
import com.yizhubao.app.activitys.AuthActivity;
import com.yizhubao.app.activitys.CancellationActivity;
import com.yizhubao.app.activitys.PayActivity;
import com.yizhubao.app.activitys.ProhibitActivity;
import com.yizhubao.app.activitys.WebActivity;
import com.yizhubao.app.activitys.user.HealthyActivity;
import com.yizhubao.app.activitys.user.SettingActivity;
import com.yizhubao.app.activitys.user.UserNewsActivity;
import com.yizhubao.app.app.YZBApp;
import com.yizhubao.app.base.BaseFragment;
import com.yizhubao.app.beans.WXLoginBean;
import com.yizhubao.app.customviews.BottomDialog;
import com.yizhubao.app.customviews.inter.CompleteCallBack;
import com.yizhubao.app.customviews.inter.OthersCallBack;
import com.yizhubao.app.events.LoginEvent;
import com.yizhubao.app.events.PayEvent;
import com.yizhubao.app.events.RecordEvent;
import com.yizhubao.app.events.WXLoginEvent;
import com.yizhubao.app.presenters.WXLoginPresenter;
import com.yizhubao.app.utils.Constants;
import com.yizhubao.app.utils.Utils;
import com.yizhubao.app.views.WXLoginView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import h.framework.pacy.P;
import h.framework.pacy.utils.LogUtil;
import h.framework.pacy.utils.PreferencesUtil;
import h.framework.pacy.utils.ToastUtil;

/**
 * 创建人: PACY
 * 创建时间: 6/4/21 10:25 AM
 * 邮箱: pengwwe457621023@qq.com
 * 类描述: 个人中心fragment
 */
public class UserFragment extends BaseFragment implements WXLoginView {
    @BindView(R.id.user_name_tv)
    TextView user_name_tv;
    @BindView(R.id.user_lable)
    ImageView user_lable;
    @BindView(R.id.baozhang_lin)
    LinearLayout baozhang_lin;
    @BindView(R.id.pay_rel)
    RelativeLayout pay_rel;

    WXLoginPresenter presenter;
    public static UserFragment newInstance(String title) {
        Bundle args = new Bundle();
        args.putString("title", title);
        UserFragment fragment = new UserFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected void initView() {
        EventBus.getDefault().register(this);
        presenter=new WXLoginPresenter();
        presenter.attachView(this);
    }

    @Override
    protected void initData() {
        LogUtil.i("initData");
        if(Utils.isLogin()){
            //登录状态
            if(Utils.isIdCard()){
                //实名认证状态
                user_lable.setVisibility(View.GONE);
//                user_name_tv.setText("张三");
                user_name_tv.setText((String) PreferencesUtil.get(Constants.REAL_NAME,""));
            }else{
                user_lable.setVisibility(View.VISIBLE);
                user_name_tv.setText("实名信息验证");
            }
            if(Utils.isPay()){
                pay_rel.setVisibility(View.GONE);
                baozhang_lin.setVisibility(View.VISIBLE);
            }else{

                if(Utils.isIdCard()){
                    pay_rel.setVisibility(View.VISIBLE);
                }else{
                    pay_rel.setVisibility(View.GONE);
                }
                baozhang_lin.setVisibility(View.GONE);
            }
        }else{
            user_lable.setVisibility(View.VISIBLE);
            user_name_tv.setText("点击验证登录");
            baozhang_lin.setVisibility(View.GONE);
            pay_rel.setVisibility(View.GONE);
        }
    }

    /**
     * 微信授权
     */
    private void loginForWX(){
        SendAuth.Req req=new SendAuth.Req();
        req.scope = "snsapi_userinfo";
        req.state = "wechat_sdk_demo_test";
        YZBApp.api.sendReq(req);
    }


    @Override
    public void onResume() {
        super.onResume();
        initData();
    }

    /**
     * 微信登录成功
     * @param data
     */
    @Override
    public void onWXLoginSuccess(WXLoginBean data) {
        LogUtil.i("微信登录成功");
        if(data.getStatus()==1){
            EventBus.getDefault().post(new LoginEvent());
//            EventBus.getDefault().post(new RecordEvent());
            initData();
            if(!Utils.isIdCard()){
                Bundle bundle=new Bundle();
                bundle.putInt("jumpType",3);
                startActivity(getActivity(),AuthActivity.class,bundle);
            }
        }else{
            Bundle bundle=new Bundle();
            bundle.putInt("type",2);
            startActivity(getActivity(), ProhibitActivity.class,bundle);
        }

    }

    @OnClick(value = {R.id.natural_uiv, R.id.baozhang_lin
            ,R.id.user_name_tv,R.id.xinli_tv,R.id.weixian_tv,R.id.juanxian_tv
            ,R.id.news_uiv,R.id.qustion_uiv,R.id.lianxikefu_uiv,R.id.jiance_uiv
            ,R.id.setting_uiv,R.id.pay_rel
    })
    public void click(View v) {
        Bundle bundle=new Bundle();
        switch (v.getId()) {
            case R.id.baozhang_lin:
                BottomDialog.DialogBuilder(getActivity()).title("已生效·保障中")
                        .message("平台为您提供的服务已经生效，背后上千名工作人员，正在实时保障关注您的健康状态。若发现您遭遇意外，将第一时间和联系人确认，并移交相关重要内容。也会协助联系人，为您处理后事 .....")
                        .completeText("知道了").completeCallBack(() -> {
                }).show();
                break;

            case R.id.user_name_tv:
               if(Utils.isLogin()){
                   if(!Utils.isIdCard()){
                       Bundle bundle2=new Bundle();
                       bundle2.putInt("jumpType",1);
                       startActivity(getActivity(),AuthActivity.class,bundle2);
                   }
               }else{
                   loginForWX();
               }
                break;

            case R.id.natural_uiv:
                bundle.putString("title","平台资质");
                bundle.putString("url", Constants.PLATFORM);
                startActivity(getActivity(), WebActivity.class,bundle);
                break;

            case R.id.xinli_tv:
                bundle.putString("title","心理辅导");
                bundle.putString("url", Constants.XINLIFUDAO);
                startActivity(getActivity(), WebActivity.class,bundle);
                break;
            case R.id.weixian_tv:
                bundle.putString("title","危险求助");
                bundle.putString("url", Constants.WEIXIANQIUZHU);
                startActivity(getActivity(), WebActivity.class,bundle);
                break;
            case R.id.juanxian_tv:
                bundle.putString("title","器官捐选");
                bundle.putString("url", Constants.QIGUAN);
                startActivity(getActivity(), WebActivity.class,bundle);
                break;

            case R.id.news_uiv:
                startActivity(getActivity(), UserNewsActivity.class);
                break;

            case R.id.qustion_uiv:
                bundle.putString("title","常见问题");
                bundle.putString("url", Constants.QUESTION);
                startActivity(getActivity(), WebActivity.class,bundle);
                break;

            case R.id.lianxikefu_uiv:
                bundle.putString("title","联系客服");
                bundle.putString("url", Constants.LIANXIKEFU);
                startActivity(getActivity(), WebActivity.class,bundle);
                break;

            case R.id.jiance_uiv:
                startActivity(getActivity(), HealthyActivity.class);
                break;

            case R.id.setting_uiv:
                startActivity(getActivity(), SettingActivity.class);
                break;

            case R.id.pay_rel:
                Bundle bundle2=new Bundle();
                bundle2.putInt("type",3);
                startActivity(getActivity(), PayActivity.class,bundle2);
                break;

        }
    }

    @Subscribe(threadMode = ThreadMode.POSTING,priority = 1)
    public void onWXLoginEvent(WXLoginEvent event){
        LogUtil.i("微信登录UserFragment=");
        Map<String,Object> map=new HashMap<>();
        EventBus.getDefault().cancelEventDelivery(event);
        map.put("code",event.getCode());
        //微信授权拿到code后发起登录
        showLoading("微信登录");
        presenter.sendWXLoginRequest(map);
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onPayEvent(PayEvent event){
        LogUtil.i("支付成功啦!");
        initData();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onLoginEvent(LoginEvent event){
        LogUtil.i("登录啦");
        initData();
    }

    @Override
    public int setLayoutResourceID() {
        return R.layout.user_fragment;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if(EventBus.getDefault().isRegistered(this)){
            EventBus.getDefault().unregister(this);
        }
    }


}
